//
//  ClockModel.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/12.
//

#import "ClockModel.h"
#import "foundQuestModel.h"

@implementation ClockModel
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
        @"ID":@[@"id"],
        @"Description":@[@"description"],
    };
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{
        @"pondModelList":[pondModelListModel class],
    };
}
- (NSMutableArray<NSString *> *)mergeList{
    if(!_mergeList){
        _mergeList = [NSMutableArray array];
    }
    return _mergeList;
}

-(CGFloat)DescriptionStrH{
    if(_DescriptionStrH == 0){
        _DescriptionStrH = [DTKTool textWithtitle:[NSString stringWithFormat:@"任务说明：%@",self.Description] andSize:CGSizeMake(kwt(335), 10000) andFout:kCFont(16)].height;
        if(_DescriptionStrH < kwt(20)){
            _DescriptionStrH = kwt(20);
        }
    }
    return _DescriptionStrH;
}
- (NSMutableArray<pondModelListModel *> *)copynamepondModelList{
    if(!_copynamepondModelList){
        _copynamepondModelList = [NSMutableArray arrayWithArray:self.pondModelList];
    }
    return _copynamepondModelList;
}

- (NSString *)tissueNameStr{
    if(!_tissueNameStr){
        NSMutableDictionary * dict = [NSMutableDictionary dictionary];
        for (pondModelListModel * pond in self.copynamepondModelList) {
            if([dict.allKeys containsObject:pond.tissueName]){
                NSMutableArray * array = [dict objectForKey:pond.tissueName];
                if (![array containsObject:pond.name]){
                    [array addObject:pond.name];
                }
            } else {
                NSMutableArray * array = [NSMutableArray array];
                [array addObject:pond.name];
                [dict setObject:array forKey:pond.tissueName];
            }
        }
        NSMutableArray * strArray = [NSMutableArray array];
        for (NSString * key in dict.allKeys) {
            NSArray * nameArray = [dict objectForKey:key];
            NSMutableArray * NewNameArray = [[NSMutableArray alloc] init];
            for (NSString * str in nameArray) {
                for (NSString * PondName in [str componentsSeparatedByString:@","]) {///去重
                    if(![NewNameArray containsObject:PondName]){
                        [NewNameArray addObject:PondName];
                    }
                }
            }
            [strArray addObject:[NSString stringWithFormat:@"%@ %@",key,[NewNameArray componentsJoinedByString:@","]]];
        }
        
        _tissueNameStr = [strArray componentsJoinedByString:@"\n"];
        
    }
    return _tissueNameStr;
}
-(CGFloat)tissueNameStrH{
    if(_tissueNameStrH == 0){
        _tissueNameStrH = [DTKTool textWithtitle:self.tissueNameStr andSize:CGSizeMake(kwt(335), 10000) andFout:kCFont(16)].height;
        if(_tissueNameStrH < kwt(20)){
            _tissueNameStrH = kwt(20);
        }
    }
    return _tissueNameStrH;
}
- (NSString *)appointorNameStr{
    if(!_appointorNameStr){
        NSString * appointorName = @"";
        NSMutableArray * nameArray = [NSMutableArray array];
        for (NSString * name in [self.appointorName componentsSeparatedByString:@","]) {
            if(![name isEqualToString:[UserInfoModel shared].realname] && name.length > 0){
                [nameArray addObject:name];
            }
        }
       
        appointorName = [nameArray componentsJoinedByString:@"、"];
        if(appointorName.length > 0){
            appointorName = [NSString stringWithFormat:@"协作人员: %@",appointorName];
        }
        _appointorNameStr = appointorName;
    }
    return _appointorNameStr;
}
- (CGFloat)appointorNameH{
    if(self.appointorNameStr.length < 1){
        return 0;
    }
    if(_appointorNameH == 0){
        _appointorNameH = [DTKTool textWithtitle:self.appointorNameStr andSize:CGSizeMake(kwt(335), 10000) andFout:kCFont(16)].height;
        if(_appointorNameH < kwt(20)){
            _appointorNameH = kwt(20);
        }
    }
    return _appointorNameH;
}
- (CGFloat)HeaderTopView{
    if(self.appointorName.length > 0){
        return kwt(85 + 9 + 15 + 10) + self.DescriptionStrH + self.tissueNameStrH + self.appointorNameH;
    }
    return kwt(85 + 9 + 15) + self.DescriptionStrH + self.tissueNameStrH;
}
- (CGFloat)HeaderView{
    if(self.superTaskModel.configureItem == 1 && self.pondModelList.count > 1){///统一配置
//        if(self.pondModelList.firstObject.DZXXtaskItemModelList.count > 0){///有多个///直接用cell绘制
            return kwt(8 + 8 + 50) + self.HeaderTopView;
//        } else {
//            return kwt(8 + 8 + 50) + self.HeaderTopView + self.superTaskModel.TYDKDXXZViewH ;
//        }
    }
    return kwt(8 + 8 + 50) + self.HeaderTopView;
}

- (NSMutableArray<NSString *> *)cardImgArray{
    if(!_cardImgArray){
        _cardImgArray = [NSMutableArray array];
    }
    return _cardImgArray;
}
- (NSMutableArray<NSString *> *)TJcardImgArray{
    if(!_TJcardImgArray){
        _TJcardImgArray = [NSMutableArray array];
    }
    return _TJcardImgArray;
}
-(NSString *)cardImg{
    if(!_cardImg){
        if(self.cardImgArray.count > 0){
            _cardImg = [self.TJcardImgArray componentsJoinedByString:@","];
        } else {
            _cardImg = @"";
        }
    }
    return _cardImg;
}
- (CGFloat)FooterPhoneViewH{
    if(self.cardImgArray.count >= 3){
        return kwt(235);
    } else {
        return kwt(235 - 90);
    }
}
- (CGFloat)FooterViewH{
    return self.FooterPhoneViewH + kwt(72);
}
- (NSMutableArray<TemplateFoundDetailsTaskItemModelListModel *> *)superTaskModelArray{
    if(!_superTaskModelArray){
        _superTaskModelArray = [NSMutableArray array];
        ///完全拷贝一份池塘
            for (TemplateFoundDetailsTaskItemModelListModel * model in self.pondModelList.firstObject.taskItemModelList) {
                NSMutableArray * pondarray = [NSMutableArray array];
                for (pondModelListModel * pond in self.pondModelList) {
//                    [pondarray addObject:[pondModelListModel yy_modelWithJSON: [pond yy_modelToJSONString]]];
                    [pondarray addObject:pond];
                }
                TemplateFoundDetailsTaskItemModelListModel * tempmodel = [TemplateFoundDetailsTaskItemModelListModel yy_modelWithJSON: [model yy_modelToJSONString]];
                tempmodel.pondModelList = pondarray;
                 [_superTaskModelArray addObject:tempmodel];
            }
    }
    return _superTaskModelArray;
}
- (TemplateFoundDetailsTaskItemModelListModel *)superTaskModel{
    if(!_superTaskModel){
        if(self.configureItem == 1){
            for (TemplateFoundDetailsTaskItemModelListModel * model in self.pondModelList.firstObject.taskItemModelList) {
                if(model.pattern == 1){///是多选
                    for (TemplateFoundDetailsItemModelListModel * listModel in model.itemModelList) {///选项
                        if(listModel.itemList.count > 0){///有子选项
                            _superTaskModel = model;
                            return _superTaskModel;
                        }
                    }
                }
            }
        }
    }
    return _superTaskModel;
}


///提交打开的hud
- (NSString *)TJHUDStr{
    if(self.configureItem == 1 || self.pondModelList.count > 1){///多池塘的统一配置
        for (TemplateFoundDetailsTaskItemModelListModel * itemModel in self.superTaskModelArray) {
            if([itemModel TjAPiHUDString].length > 0){
                return [NSString stringWithFormat:@"%@ - %@",itemModel.name,[itemModel TjAPiHUDString]];
            }
        }
    } else {
        for (pondModelListModel * model in self.pondModelList) {
            for (TemplateFoundDetailsTaskItemModelListModel * itemModel in model.taskItemModelList) {
                if([itemModel TjAPiHUDString].length > 0){
                    return [NSString stringWithFormat:@"%@ - %@",model.name,[itemModel TjAPiHUDString]];
                }
            }
        }
    }
    return @"";
}
- (NSMutableDictionary *)TjDict{
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.cardImg forKey:@"cardImg"];
    [dict setObject:@(self.configureItem) forKey:@"configureItem"];
    [dict setObject:self.TJvoiceUrl forKey:@"voiceUrl"];
    [dict setObject:self.mergeList forKey:@"mergeList"];
    NSMutableArray * pondModelList = [NSMutableArray array];
    for (pondModelListModel * model in self.pondModelList) {
        if(self.configureItem == 1 && self.pondModelList.count > 1){
            model.taskItemModelList = self.superTaskModelArray;///多池塘统一重新赋值
        }
        [pondModelList addObject:[model TjDKDict]];
    }
    [dict setObject:pondModelList forKey:@"pondModelList"];

    return dict;
}

- (void)setVoiceUrl:(NSString *)voiceUrl{
    _voiceUrl = voiceUrl;
    NSError * error ;
    if(voiceUrl.length < 1){
        return;
    }
    AVURLAsset* audioAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:voiceUrl] options: @{AVURLAssetPreferPreciseDurationAndTimingKey:@(YES)}];
    [audioAsset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            AVKeyValueStatus status = [audioAsset statusOfValueForKey:@"duration" error:nil];
            if(status == AVKeyValueStatusLoaded){
                CMTime audioDuration = audioAsset.duration;
                float audioDurationSeconds = CMTimeGetSeconds(audioDuration);
                self.voiceDuration = [NSString stringWithFormat:@"%.0f\"",round(audioDurationSeconds)];
                if(self.openDurationBlock){
                    self.openDurationBlock(self.voiceDuration);
                }
            }
        });
    }];
    
}
@end
